﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChoViet.Model;
using ChoViet.Site.ViewModels;
using ChoViet.Lib.Common;
using System.Linq.Dynamic;
using ChoViet.Site.Properties;
namespace ChoViet.Site.Controllers
{
    public class ArticleController : Controller
    {
        //
        // GET: /Article/
        ChoVietDataContext eDataContext = new ChoVietDataContext();
        public ActionResult Index(int id)
        {
            try
            {
                ChoVietDataContext db = new ChoVietDataContext();
                string order = UserState.getUserStateFromRequest("article.order", "order", "ID").ToString();
                string orderDir = UserState.getUserStateFromRequest("article.orderDir", "orderDir", "desc").ToString();
                int catId = id;
                string keyword = UserState.getUserStateFromRequest("article.Keyword", "keyword", "").ToString();
                var pageNumber = Convert.ToInt32(UserState.getUserStateFromRequest("article.PageNumber", "page", 1));
                var pageIndex = pageNumber - 1;
                int pageSize = int.Parse(UserState.getUserStateFromRequest("article.pageSize", "PageSize", 10).ToString());
                pageSize = pageSize > 10 ? 10 : pageSize;
                var articles = ArticleManager.GetArticles(catId, keyword, -1, 1, order, orderDir, pageIndex, pageSize);
                var category = catId > 0 ? db.ArticleCategories.Where(e => e.ID == catId).FirstOrDefault() : null;

                var volumeRootImageUrl = Settings.Default.SiteRootUrl + Settings.Default.VolumeImagePreviewUrlPrefix;
                var volumeImagePath = Settings.Default.SiteRootPath + Settings.Default.VolumeImageRootPath;

                return View(new ArticleIndexViewModel
                {
                    Articles = articles,
                    ArticleCategory = category,
                    Keyword = keyword,
                    RootImageUrl = Settings.Default.SiteRootUrl + Settings.Default.ArtileImagePreviewUrlPrefix,
                    ImagePath = Settings.Default.SiteRootPath + Settings.Default.ArticleImageRootPath
                });
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                return View();
            }
        }

        [ChildActionOnly]
        public ActionResult TopNews(int hotnewNumber, int languageId)
        {
            try
            {
                TopNewsViewModel model = new TopNewsViewModel();
                model.Articles = eDataContext.Articles.Where(e => e.PubYN == true && e.HotYN == true)
                                                      .Where(e => e.LanguageID == languageId)  
                                                      .OrderByDescending(e => e.AddDate).Take(hotnewNumber).ToList();
                return View(model);
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                return View();
            }
        }

        [ChildActionOnly]
        public ActionResult GetNewsByCat(int catID, int number)
        {
            try
            {
                GetNewsByCatViewModel model = new GetNewsByCatViewModel();
                model.Articles = eDataContext.Articles.Where(e => e.PubYN == true && e.TypeID == ArticleListType.NormalArticle && e.CatID == catID)
                                                      .OrderByDescending(e => e.AddDate)
                                                      .Take(number).ToList();
                model.ArticleCategory = eDataContext.ArticleCategories.Where(e => e.ActiveYN == true && e.ID == catID).FirstOrDefault();
                return View(model);
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                return View();
            }
        }

        public ActionResult Details(int id)
        {
            try
            {
                ArticleDetailViewModel model = new ArticleDetailViewModel();
                model.Article = eDataContext.Articles.Where(e => e.ID == id && e.PubYN == true).FirstOrDefault();
                model.ArticlesSameCate = eDataContext.Articles.Where(e => e.CatID == model.Article.CatID && e.PubYN == true)
                                                              .OrderByDescending(e => e.AddDate)
                                                              .Take(int.Parse(Settings.Default.ArcticleSameCateNumber.ToString()))
                                                              .ToList();
                return View(model);
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                return View();
            }
        }

        [ChildActionOnly]
        public ActionResult GetQuestion(int catID, int number)
        {
            try
            {
                GetQuestionViewModel model = new GetQuestionViewModel();
                model.Articles = eDataContext.Articles.Where(e => e.PubYN == true && e.TypeID == ArticleListType.NormalArticle && e.CatID == catID)
                                                      .OrderByDescending(e => e.AddDate)
                                                      .Take(number).ToList();
                return View(model);
            }
            catch (Exception exp)
            {
                ErrorLog.WriteLog(exp);
                return View();
            }
        }

        //[ChildActionOnly]
        /*
        public ActionResult AboutUs(int id, string title)
        {
            var db=new ChoVietDataContext();
            var a = db.Articles.Where(e => e.Category.TypeID == id).OrderByDescending(e=>e.ID).SingleOrDefault();
            ViewData["Title"] = string.IsNullOrEmpty(title) ? "Giới thiệu" : title;
            return View(a);
        }
        
        public ActionResult Introduction(int id)
        {
            var db = new SDT.Model.SDTDataContext();
            ArticleDetailViewModel a = new ArticleDetailViewModel();
            a.Article = db.Articles.Where(e => e.ID == id).SingleOrDefault();            
            return View(a);
        }
        */
    }
}
